package com.weyoung.admin.controller;
import com.weyoung.admin.dto.req.*;
import com.weyoung.admin.mapper.UserDetailMapper;
import com.weyoung.admin.service.UserDetailService;
import com.weyoung.common.consatant.result.Result;
import com.weyoung.common.model.entity.UserDetailDO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户详细接口管理
 */
@RestController
@RequestMapping("admin/userdetail")
@Tag(name = "Web端-用户详细接口")
@Slf4j
@RequiredArgsConstructor
public class UserDetailController {

    private final UserDetailService userDetailService;


    @Autowired
    private UserDetailMapper userDetailMapper;

    /**
     * 用户详细信息新增或修改
     * @param requestParam
     * @return Void
     */
    @PostMapping("/add")
    @Operation(summary = "用户详细信息上传")
    public Result<Void> add1(@RequestBody UserDetailAddDTO requestParam){
        userDetailService.add1(requestParam);
        return Result.success();
    }



    @PostMapping("/update")
    @Operation(summary = "用户详细信息修改")
    public Result<Void> update(@RequestBody UserDetailAddDTO requestParam){
        userDetailService.update(requestParam);
        return Result.success();
    }

    /**
     * 用户详细信息删除（逻辑删除）
     *@param id
     * @return Void
     *
     * 这里存在问题，逻辑删除完成后，is_deleted 变成1， 但是查找时仍旧能找到（可能是配置的问题）
     */
    @DeleteMapping("/delete1")
    @Operation(summary = "用户详细信息删除（逻辑删除）")
    public Result<Void> delete1(@RequestParam("id") Integer id){
        userDetailService.delete(id);
        return Result.success();
    }

    /**
     * 用户详细信息删除（彻底删除）
     *@param id
     * @return Void
     */
    @DeleteMapping("/delete2")
    @Operation(summary = "用户详细信息删除（彻底删除）")
    public Result<Void> delete2(@RequestParam("id") Integer id){
        userDetailService.removeById(id);
        return Result.success();
    }

    /**
     * 获取所有用户详细信息
     *
     * @return List<UserDetailDO>
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有用户详细信息")
    public Result<List<UserDetailDO>> list(){
        return Result.success(userDetailMapper.finlist());
    }


    /**
     * 根据id获取用户详细信息
     * @Param id 用户id
     * @return UserDetailDO
     */
    @GetMapping("/getUserDetailById")
    @Operation(summary = "根据id获取用户信息")
    public Result<UserDetailDO> getUserDetail1(@RequestParam("id") Integer id){
        return Result.success(userDetailService.getById(id));
    }




    /**
     * 根据id修改用户权限
     * @Param 用户id
     * @return
     */
    @PostMapping("/setUserRoleById")
    @Operation(summary = "根据用户ID修改用户权限")
    public Result<Boolean> setUserRoleById(@RequestBody UserSetRoleDTO userSetRoleDTO){
        Boolean flag = userDetailService.updateUserRoleById(userSetRoleDTO);
        return Result.success(flag);
    }

}
